// webpack 和 webpack-alias 都会引用该文件

var path = require('path');
var webpack = require('webpack');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var autoprefixer = require('autoprefixer');

var extractLESS = new ExtractTextPlugin('main.css', {
	allChunks: true
});

module.exports = {
	entry: {
		'main': path.resolve('www/static/js/app.js')
	},
	output: {
		path: 'www/static/build/',
		filename: '[name].js',
		chunkFilename: 'chunk.[id].[name]_[chunkhash:7].js',
		publicPath: '/static/pkg/'
	},
	externals: [{
		bluebird: 'Promise',
		react: 'React',
		'react-dom': 'ReactDOM'
	}],
	resolve: {
		root: path.resolve('output/node_modules'),
		alias: {
			view: __dirname + '/view',
			helper: __dirname + '/view/helper',
			components: __dirname + '/view/components'
		}
	},

	module: {
		loaders: [{
			test: /\.jsx?$/,
			exclude: /(node_modules)/,
			loader: 'babel',
			query: {
				"presets": [
					["env", {
						"targets": {
							"browsers": ["ie >= 9"]
						},
						"loose": true,
						"modules": "commonjs"
					}],
					"stage-0",
					"react",
					"bluebird"
				],
				"plugins": [
					"transform-decorators-legacy", [
						"import", [{
							"libraryName": "antd",
							"style": true
						}, {
							"libraryName": "antd-mobile",
							"style": true
						}]
					]
				]
			}
		}, {
			test: /\.(less|css)$/,
			loader: extractLESS.extract([
				'css?sourceMap',
				'postcss?sourceMap',
				'less?sourceMap'
			])
		}, {
			test: /\.(ttf|woff)\b/i,
			loader: 'url',
			query: {
				emitFile: false,
				name: '[path][name].[ext]',
				publicPath: '/'
			}
		}, {
			test: /\.(eot|svg)\b/i,
			loader: 'file',
			query: {
				emitFile: false,
				name: '[path][name].[ext]',
				publicPath: '/'
			}
		}, {
			test: /\.(png|gif|jpg)\b/i,
			include: path.resolve(__dirname, 'www'),
			loader: 'file',
			query: {
				emitFile: false,
				name: '[path][name].[ext]',
				context: path.resolve(__dirname, 'www'),
				publicPath: '/'
			}
		}, {
			test: /\.(png|gif|jpg)\b/i,
			exclude: path.resolve(__dirname, 'www'),
			loader: 'file',
			query: {
				name: '[path][name].[ext]'
			}
		}, {
			test: /\.json$/i,
			loader: 'json-loader'
		}],
		noParse: []
	},

	plugins: [
		/*new webpack.optimize.UglifyJsPlugin({
			compress: {
				warnings: false
			}
		}),*/
		new webpack.DefinePlugin({
			"process.env": {
				ENV: JSON.stringify("browser"),
				NODE_ENV: JSON.stringify("production")
			}
		}),
		new webpack.ContextReplacementPlugin(/moment[\/\\]locale$/, /zh\-cn/),
		// antd/antd-mobile 的 themes 替换。用 less 的 modifyVars 替换效率太低，用模块替换更好
		new webpack.NormalModuleReplacementPlugin(/(antd|antd-mobile)[\/\\]lib[\/\\]style[\/\\]themes[\/\\]default/, function(opt) {
			var request = opt.request.split('!');
			var oldPath = request.pop();
			var newPath = path.resolve(__dirname + `/www/static/vendor/${oldPath.indexOf('antd-mobile') > -1 ?
				 'antd-mobile': 'antd'}/themes.less`);
			request.push(newPath);
			opt.request = request.join('!');
		}),
		extractLESS
	],

	postcss: function() { // postcss 插件
		// 该函数会多次执行
		// console.log('postcss')
		return [autoprefixer({
			browsers: ['Android >= 4', 'iOS >= 7']
		})];
	},

	devtool: '#source-map'
};